package com.qf.dmf;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;

/**
 * 当面付统一下单
 */
public class AlipayTradePrecreate {
    public static void main(String[] args) throws AlipayApiException {
        String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCyFq14+keWgROSycJdloSy3G3YR7BSzvIzaegHDwzWIrz+FHQ1Bpt2yC+972Oc3MLvzBupBjjsZVvlJcuElbQ/w9XBD71vhNlecCLHLYOzpJWP2UsoMaC3hZ6OvXC0qEeFTRmS8MiAvP8x4B5by+LjykQPKeemfOJHqSlZzcXCoh2zqOvVDQVzMjNKozM3Tni1iOuaKjs9wSYX2XcCtrqGnDvO58kecub8PgEwHHi2xfKzDVd3Kn9om2Mg+mUJBHPLU4spfcb7NePe+TekSjueZ63dzvubs1qs53p8iC3KaY2KvMpjQrWpOxewsFAWU4wVIPTuN7LnRR93eQfMAF1tAgMBAAECggEAXjr7nRipCmzP44qYKpb7LfzOrnL4TEiuBSBTonCHNUL6R2KhPnF5jzdLc6+YQKbbCrEwLZZaRlM3dZ8y+buQvXCsyNPAQ6LCtWKWNFfT6Rj7/jpWtzTTOmgcRqDcYurrtQ/fqU14eg4G5egViDLwoeEjc/aTWReZT5ulgpwLuPjW0viXowdCVUlZJb4kRk87eBdxgyFO5Xv0xYfHRydzKuXO6kMohLZUZOiUmXxeBqVp1P8b/w/TE4WIrkVythMOVF2gnBQvbH5YI6qHMe6Z86D2ujxfvQERCjciHrS6uRQgU5+5Qz3qu649KK0OivmmhnMs3u/hZZthQ09zYvv1rQKBgQDVoYcMVCVgW21dL/prV1Z8sAdu5axXanAE3WzVcvJqpzPMOJ0Qt86WJ521uT0cdSLLtuaJ24j1S2lCsmSfd2JqR07dEmUL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oJWONZko8L00ZVmNQcLSkpL1zmcUh5l4KJVtl6BV3r2R5kt/r+AMYfit3LH2lHyxZFrZiBlt3rywA/+wZuKZ8Hjt1hZeTYIWQkFqnxBzAMMXwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000128678566");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        model.setOutTradeNo("20240320010101001");
        model.setTotalAmount("88.88");
        model.setSubject("Iphone6 16G");
        request.setBizModel(model);
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
            System.out.println("将这个url转换为二维码图片即可支付："+response.getQrCode());
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }
}
